package com.dtt.company.department.service;

import java.util.List;

import com.dtt.company.department.dao.DepartmentDao;
import com.dtt.company.department.model.DepartmentModel;
import com.dtt.company.employee.dao.EmployeeDao;
import com.dtt.company.employee.model.EmployeeModel;
import com.dtt.company.util.FmtEmpty;

public class DepartmentService {
private DepartmentDao dao=new DepartmentDao();
private EmployeeDao  employeeDao=new EmployeeDao();
public String insert(DepartmentModel model) {
	//添加的逻辑主键（账号）存在时，不允许添加
	DepartmentModel mdb=selectModel(model);
	if(mdb==null) { 
		Integer result=dao.insert(model);
		return String.valueOf(result);
	} 
	return "E";
}

public String delete(DepartmentModel model) {
	EmployeeModel m0=new EmployeeModel();
	m0.setDepCode(model.getCode());
	int count=employeeDao.selectCount(m0);
	if(count>0)
		return "E";
	Integer result=dao.delete(model);
	return String.valueOf(result);
}

public String update(DepartmentModel model) {
	Integer result=dao.update(model);
	return String.valueOf(result);
}

/**
 *多条件模糊查询出一些记录
 * @author Dtt
 * @return
 */
public List<DepartmentModel> select(DepartmentModel model) {
	String code=model.getCode();
//	System.out.println("code==="+code);
	if(code!=null)
		model.setCode("%"+code+"%") ;
	String name=model.getName();
//	System.out.println("name==="+name);
	if(name!=null)
		model.setName("%"+name+"%");
	return dao.select(model);
}

/**
 * 根据逻辑主键查询出唯一的记录，主键不存在返回null
 * @author Dtt
 * @return
 */
public DepartmentModel selectModel(DepartmentModel model) {
	DepartmentModel temp=new DepartmentModel();
	temp.setCode(model.getCode());
	List<DepartmentModel> list = dao.select(temp);
	if(FmtEmpty.isEmpty(list)) {
		return null;
	}
	return list.get(0);
	
}
//分页：根据多条件的模糊查询的数据总数
public int selectCount(DepartmentModel model) {
	DepartmentModel mdb=new DepartmentModel();
	String code=model.getCode();
	mdb.setCode(code == null ? "%%":"%"+code+"%");
	String name=model.getName();
	mdb.setName(name == null ? "%%":"%"+name+"%");
	mdb.setPageOn(false);
	return dao.selectCount(mdb);
	
}
}
